package com.ttl.bc.ic.stktransfer2;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.util.LabelValueBean;


import com.ttl.bc.ic.stklocation.BCStkLocationDAO;
import com.ttl.bc.ic.stklocation.BCStkLocationPopup;
import com.ttl.bc.ic.stktransfer.BCStkTransfer;
import com.ttl.common.util.CommonUtils;
import com.ttl.common.util.Page;

public class BCStkTransfer2BO {
	
	public void showFind(HttpServletRequest request) throws UnsupportedEncodingException {
//		String userName = (String)request.getSession().getAttribute("userName");			

		String dept = request.getParameter("dept");
		
		String	itemFind = "";
		if(request.getParameter("itemFind")!=null) {
			itemFind = ((String)request.getParameter("itemFind"));
			itemFind = new String(itemFind.getBytes("ISO8859_1"),"UTF-8");
			request.setAttribute("itemFind", itemFind.trim());
		}		
		
		String hql = "";
		if(itemFind==null||itemFind.trim().equals("")) {
			hql = "FROM BCStkTransfer2 " +
					 "WHERE docNo not like '%"+dept.toUpperCase()+"%' " +
					 "ORDER BY docNo desc";			
		}else {
			hql = "FROM BCStkTransfer2  " +
					 "WHERE docNo not like '%"+dept.toUpperCase()+"%' " +
					 "and docNo like '%"+itemFind.trim()+"%' "+					 
					 "ORDER BY docNo desc";			
		}
		
		System.out.println(hql);
		Page.setPage(request, hql);
	}
	
	public void setShelfToJSP(ActionForm form, HttpServletRequest request) {
		String dept= request.getParameter("dept");
		
		ArrayList<LabelValueBean> toShelfList = setToShelfList(dept); 
		ArrayList<LabelValueBean> fromShelfList = setFromShelfList(dept);
		
		BCStkTransfer2Form a = (BCStkTransfer2Form)form;
		a.setFromShelfList(fromShelfList);
		a.setToShelfList(toShelfList);
	}

	public ArrayList<LabelValueBean> setFromShelfList(String dept) {
		
		ArrayList<LabelValueBean> fromShelfList = new ArrayList<LabelValueBean>();
		if (dept.equalsIgnoreCase("PD") || dept.equals("12") ) {
			fromShelfList.add(new LabelValueBean("Central Store", "10"));
			fromShelfList.add(new LabelValueBean("Chemical YD", "16"));
			fromShelfList.add(new LabelValueBean("Chemical FF", "17"));
		} else if (dept.equalsIgnoreCase("YD") || dept.equals("16") ) {
			fromShelfList.add(new LabelValueBean("Central Store", "10"));
			fromShelfList.add(new LabelValueBean("Chemical PD", "12"));			
			fromShelfList.add(new LabelValueBean("Chemical FF", "17"));			
		} else if (dept.equalsIgnoreCase("FF") || dept.equals("17")) {
			fromShelfList.add(new LabelValueBean("Central Store", "10"));
			fromShelfList.add(new LabelValueBean("Chemical PD", "12"));
			fromShelfList.add(new LabelValueBean("Chemical YD", "16"));						
		} else {
			fromShelfList.add(new LabelValueBean("Central Store", "10"));
			fromShelfList.add(new LabelValueBean("Chemical PD", "12"));
			fromShelfList.add(new LabelValueBean("Chemical YD", "16"));
			fromShelfList.add(new LabelValueBean("Chemical FF", "17"));			
		}		
		return fromShelfList;
	}

	public ArrayList<LabelValueBean> setToShelfList(String dept) {
		
		ArrayList<LabelValueBean> toShelfList = new ArrayList<LabelValueBean>();
		
		if (dept.equalsIgnoreCase("PD") || dept.equals("12")) {
			toShelfList.add(new LabelValueBean("Chemical PD", "12"));	
		} else if (dept.equalsIgnoreCase("YD") || dept.equals("16")) {
			toShelfList.add(new LabelValueBean("Chemical YD", "16"));
		} else if (dept.equalsIgnoreCase("FF") || dept.equals("17")) {
			toShelfList.add(new LabelValueBean("Chemical FF", "17"));
		} else {
			toShelfList.add(new LabelValueBean("Central Store", "10"));
			toShelfList.add(new LabelValueBean("Chemical PD", "12"));
			toShelfList.add(new LabelValueBean("Chemical YD", "16"));
			toShelfList.add(new LabelValueBean("Chemical FF", "17"));			
		} 		
		return toShelfList;
	}

	@SuppressWarnings("rawtypes")
	public boolean add(ActionForm form, HttpServletRequest request,
			HttpServletResponse response) {
		String user = request.getSession().getAttribute("userName").toString();
		
		BCStkTransfer2 s = setBean(form);
		s.setCreatorCode(user);
		
		BCStkTransfer2DAO dao = new BCStkTransfer2DAO();
		String hql = "FROM BCStkTransfer2 WHERE docNo='"+s.getDocNo()+"' ";
		List al = dao.find(hql);
		if(al==null || al.size()>0) {
			String maxDocNo = CommonUtils.getMaxDocNoBCStkTransfer2();
			s.setDocNo(maxDocNo);			
		}
		dao.add(s);
		request.setAttribute("docNoPrint", s.getDocNo());
		return true;
	}

	@SuppressWarnings("unchecked")
	public BCStkTransfer2 setBean(ActionForm form) {
		BCStkTransfer2Form sf = (BCStkTransfer2Form)form;

		BCStkTransfer2 s = new BCStkTransfer2();
		s.setDocNo(sf.getDocNo());
		s.setDocDate(CommonUtils.convertStringToDate(sf.getDocDate()));
		s.setDepartCode(sf.getDepartCode());
		try {
//			cbean.setRequirement(new String(cform.getRequirement().getBytes("ISO8859_1"),"UTF-8"));
			s.setMyDescription(new String( sf.getMyDescription().getBytes("ISO8859_1"),"UTF-8" ));			
		}catch (Exception e) {
			
		}
		
		s.setIsConfirm(0);
		s.setBillStatus(0);
		s.setIsCancel(0);
		s.setIsProcessOK(0);
		s.setIsCompleteSave(1);
		s.setCreateDateTime(new Date());
		
		double sumOfQty = 0;
//		ArrayList<BCStkTransferSub2> al = new ArrayList<BCStkTransferSub2>();
		HashSet hs = new HashSet();
		for (int i = 0; i < sf.getItemCode().length; i++) {
			BCStkTransfSub2 sub = new BCStkTransfSub2();
		//	sub.setDocNo(s.getDocNo());
			sub.setDocDate(s.getDocDate());
			sub.setDepartCode(s.getDepartCode());
			
			sub.setItemCode(sf.getItemCode()[i]);
			sub.setIttl_itemName(sf.getIttl_itemName()[i]);
			try {
				sub.setMyDescription(new String( sf.getMyDescriptionSub()[i].getBytes("ISO8859_1"),"UTF-8"));
			}catch (Exception e) {
				
			}
			sub.setFromWH(sf.getFromWH());
			sub.setFromShelf(sf.getFromShelf());
			sub.setToWH(sf.getToWH());
			sub.setToShelf(sf.getToShelf());
			sub.setQty(sf.getQty()[i]);
			sub.setRemainQty(sub.getQty());
			sub.setUnitCode(sf.getUnitCode()[i]);
			
			sub.setTransState(0);
			sub.setIsCancel(0);
			sub.setLineNumber(i);
			sub.setRemainQty(sub.getQty());
			
			sub.setPrice(0.0);
			sub.setSumOfCost(0.0);
			sub.setAmount(0.0);
			
			
			sub.setBcStkTransfer2(s);
			hs.add(sub);
//			al.add(sub);
			
			sumOfQty +=sub.getQty();
		}
		s.setSumOfQty(sumOfQty);
		s.setBcStkTransfSubs(hs);
//		s.setBcStkTransferSubs(al);
		return s;
	}

	@SuppressWarnings("unchecked")
	public boolean view(ActionForm form, HttpServletRequest request,
			HttpServletResponse response) {
		
		String docNo = request.getParameter("docNo");
		String hql = "FROM BCStkTransfer2 WHERE docNo='"+docNo+"'";
		ArrayList<BCStkTransfer2> al = (ArrayList<BCStkTransfer2>)new BCStkTransfer2DAO().find(hql);
		if(al.size()>0) {
			
			BCStkTransfer2 s = al.get(0);
			request.setAttribute("docDate", (""+s.getDocDate()).substring(0, 10));
			request.setAttribute("s", s);
			request.setAttribute("sub", s.getBcStkTransfSubs());
			
			String fromShelf = "";
			String toShelf = "";
			Iterator it = s.getBcStkTransfSubs().iterator();
			while (it.hasNext()) {
				BCStkTransfSub2 sub = (BCStkTransfSub2) it.next();
				fromShelf = sub.getFromShelf();
				toShelf=sub.getToShelf();
				
			}
			ArrayList<LabelValueBean> toShelfList = setToShelfList(toShelf); 
			ArrayList<LabelValueBean> f = setFromShelfList(toShelf);			
			ArrayList<LabelValueBean> fromShelfList = new ArrayList<LabelValueBean>(); 
			for (int i = 0; i < f.size(); i++) {
				LabelValueBean l = (LabelValueBean)f.get(i);
				if(l.getValue().equals(fromShelf)) {
					fromShelfList.add(l);
				}
			}
			
			request.setAttribute("countId", s.getBcStkTransfSubs().size());
			
			request.setAttribute("fromShelf", fromShelf);
			request.setAttribute("toShelf", toShelf);
			request.setAttribute("toShelfList", toShelfList);
			request.setAttribute("fromShelfList", fromShelfList);
			
			return true;
		}else {
			return false;
		}
	}

	public boolean update(ActionForm form, HttpServletRequest request,
			HttpServletResponse response) {
		
		BCStkTransfer2 s = setBean(form);
		
		String dept = request.getParameter("dept").toString();
		request.setAttribute("message", "แก้ไขข้อมูลใบขอโอนเรียบร้อย");
		request.setAttribute("dept", dept);
		request.setAttribute("pageForward", "<meta http-equiv='refresh' content='1;" +
				"url=stktransfer2.do?mode=showFind&dept="+dept+"&docNo="+s.getDocNo()+"'>");	
		request.setAttribute("itemFind", s.getDocNo());

		return 	new BCStkTransfer2DAO().update(s.getDocNo(),s);
	}

	public boolean delete(ActionForm form, HttpServletRequest request,
			HttpServletResponse response) {
		
		BCStkTransfer2 s = setBean(form);
	
		return 	new BCStkTransfer2DAO().cancel(s.getDocNo());
	}

	public void popupItems(HttpServletRequest request) throws Exception {
		String	itemFind = "";
		if(request.getParameter("itemFind")!=null) {
			itemFind = ""+((String)request.getParameter("itemFind"));
			itemFind = new String(itemFind.getBytes("ISO8859_1"),"UTF-8").trim();
			request.setAttribute("itemFind", itemFind);
		}
		
		String shelfCode = request.getParameter("shelfCode");

		String hql2 = "SELECT l.itemCode, i.name1 as itemName, l.qty, u.name as unitName " +
				" FROM BCItemBean i, BCStkLocationBean l, BCItemUnitBean2 u " +
				" WHERE i.code=l.itemCode " +
				" and u.code=l.unitCode " +
				" and l.shelfCode='"+shelfCode+"'" +
		" and (l.itemCode like '2-%' or l.itemCode like '3-%' )" +
		" and l.itemCode like '%"+ itemFind+"%'";
		
		hql2 = "FROM ViewBCStkLocation " +
				" WHERE shelfCode='"+shelfCode+"'"+
				" and (itemCode like '2-%' or itemCode like '3-%' )" +
				" and itemCode like '%"+ itemFind+"%'";
		
		request.setAttribute("shelfCode", shelfCode);
		new Page().setPage(request, hql2);		
	}
}
